<?php

class PluginnahoWikiEditForm extends sfForm
{

  /**
   *
   * @var nahoWikiRevision
   */
  protected $object;

  /**
   *
   * @param $revision
   * @param $options
   * @param $CSRFSecret
   * @return unknown_type
   */
  public function __construct(nahoWikiRevision $revision, $defaults = array(), $options = array(), $CSRFSecret = null)
  {
    var_dump($revision->getContent());
    $this->object = $revision;

    parent::__construct($defaults, $options, $CSRFSecret);
  }

  /**
   * (non-PHPdoc)
   * @see vendor/symfony/lib/form/sfForm#configure()
   */
  public function setup()
  {
    sfApplicationConfiguration::getActive()->loadHelpers('I18N');

    $this->setWidgets(array(
      'page'            => new sfWidgetFormInputHidden(array('default' => $this->object->getnahoWikiPage()->getName())),
      'revision'        => new sfWidgetFormInputHidden(array('default' => $this->object->getRevision())),
      'content'         => new sfWidgetFormTextarea(array('default' => strval($this->object->getContent())), array('rows' => 20, 'cols' => 80)),
      'comment'         => new sfWidgetFormInput(array('default' => $this->object->isNew() ? __('Creation') : $this->object->getComment()), array('size' => 80)),
      'submit'          => new sfWidgetFormInput(array('type' => 'submit', 'label' => __('Save changes')), array('class' => 'submit-button', 'value' => __('Save changes'))),
      'request-preview' => new sfWidgetFormInput(array('type' => 'submit', 'label' => __('Preview')), array('class' => 'preview-button', 'value' => __('Preview'))),
    ));

    $this->setValidators(array(
      'page'            => new sfValidatorPass(),
      'revision'        => new sfValidatorPass(),
      'content'         => new sfValidatorString(array('required' => true), array('required' => __('The content cannot be left blank. Please enter some text...'))),
      'comment'         => new sfValidatorString(array('required' => true), array('required' => __('Please enter a comment describing your changes shortly.'))),
      'submit'          => new sfValidatorPass(),
      'request-preview' => new sfValidatorPass(),
    ));

    if (!$this->getOption('can-edit'))
    {
      unset($this['submit'], $this['request-preview']);
      $this->setValidator('content', new sfValidatorCallback(array('callback' => array($this, 'checkValue'), 'arguments' => array('content'))));
      $this->setValidator('comment', new sfValidatorCallback(array('callback' => array($this, 'checkValue'), 'arguments' => array('comment'))));
    }
    elseif (!$this->object->isNew())
    {
      $this->setDefault('comment', '');
    }

    $this->widgetSchema->setNameFormat(sfConfig::get('app_nahoWikiPlugin_edit_form_name', 'wiki_edit').'[%s]');
  }

  /**
   *
   * @return unknown_type
   */
  public function save()
  {
    $revision = $this->object;
    $page = $revision->getnahoWikiPage();

    if (!$page->isNew())
    {
      $revision->archiveContent();

      $revision = new nahoWikiRevision();
      $revision->initUserName();
      $revision->setRevision($page->getLatestRevision()+1);
      $page->addnahoWikiRevision($revision);
    }

    $revision->setContent($this->getValue('content'));
    $revision->setComment($this->getValue('comment'));

    if (!$this->getValue('request-preview'))
    {
      $page->setLatestRevision($revision->getRevision());
      $page->save();

      return true;
    }

    return false;
  }

  /**
   * Checks that the value has not changed
   *
   * @param $value
   * @param $name
   * @throws sfValidatorError
   */
  public function checkValue($value, $name)
  {
    $objectValue = call_user_func($this->object, 'get'.ucfirst($name));

    if (strval($value) != strval($objectValue))
    {
      throw new sfValidatorError($this->getValidator($name), 'error');
    }
  }

}
